package util_html

import (
	"bytes"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/bitly/go-simplejson"
	"html/template"
	"strconv"
	"strings"
)

type Tables struct {
	Code       string            //列表的编码
	Heads      []string          //列表的表头
	Datas      []orm.Params      //列表的数据
	Cols       []string          //列表表头对应的sql字段
	Page       int               //列表当前页码
	TotalPages int               //列表总页数
	Mount      int               //列表每页显示数
	Pages      map[int]int       //列表总页数数组
	BPage      int               //列表前一页数
	APage      int               //列表后一页数
	Total      int               //列表总共多少行
	Buttons    map[string]Button //列表按钮字典
	But        string            //列表字符表示对应表talbes的button字段
	Length     int               //列表表头长度
}

type Button struct {
	Name    string //按钮名字
	Onclick string //按钮onclick事件
	Href    string
	Class   string //按钮样式
}

//code代表列表的编码
func GetListGrid(code string, page int, args string, sousuo string) (div string) {
	var tables Tables
	buff := bytes.NewBufferString("")
	t, err := template.ParseFiles("views/frame/_tables.html")
	if err != nil {
		beego.Debug(err)
	}
	var maps []orm.Params
	var datas []orm.Params
	oram1 := orm.NewOrm()
	oram1.Raw("select * from tables where code=?", code).Values(&maps)

	tables.Page = page
	tables.Heads = strings.Split(maps[0]["head"].(string), ",")
	tables.Length = len(tables.Heads) + 1
	tables.Cols = strings.Split(maps[0]["col"].(string), ",")
	tables.Code = maps[0]["code"].(string)
	tables.But = maps[0]["button"].(string)
	tables.Mount, _ = strconv.Atoi(maps[0]["mount"].(string))
	sql := maps[0]["sql"].(string)
	// beego.Debug(sql)
	if sousuo != "" {
		sql = "select * from (" + sql + ")z where " + sousuo
	}
	if len(args) == 0 {
		// beego.Debug(strings.TrimSpace("select count(*) as cc from ( " + sql + ")zvcasdfewr"))
		oram1.Raw(strings.TrimSpace("select count(*) as cc from ( " + sql + ")zvcasdfewr")).Values(&maps)
	} else {
		oram1.Raw(strings.TrimSpace("select count(*) as cc from ( "+sql+")zvcasdfewr"), args).Values(&maps)
	}
	tables.Total, _ = strconv.Atoi(maps[0]["cc"].(string))
	remainder := tables.Total % tables.Mount
	if remainder == 0 {
		tables.TotalPages = tables.Total / tables.Mount
	} else {
		tables.TotalPages = tables.Total/tables.Mount + 1
	}
	tables.Pages = make(map[int]int)
	for i := 0; i < tables.TotalPages; i++ {
		tables.Pages[i+1] = i + 1
	}

	tables.BPage = tables.Page - 1
	tables.APage = tables.Page + 1
	if tables.BPage < 1 {
		tables.BPage = 1
	}
	if tables.APage > tables.TotalPages {
		tables.APage = tables.TotalPages
	}

	sql = "select * from (" + sql + " )z limit "
	sql = sql + strconv.Itoa((page-1)*tables.Mount) + "," + strconv.Itoa(tables.Mount)
	if len(args) == 0 {
		oram1.Raw(strings.TrimSpace(sql)).Values(&datas)
	} else {
		oram1.Raw(strings.TrimSpace(sql), args).Values(&datas)
	}

	tables.Datas = datas

	js, err := simplejson.NewJson([]byte(tables.But))
	if err == nil {
		a, err := js.Get("Button").Array()
		if err == nil {
			tables.Buttons = make(map[string]Button)
			for i := 0; i < len(a); i++ {
				var b Button
				b.Name, _ = js.Get("Button").GetIndex(i).Get("Name").String()
				b.Onclick, _ = js.Get("Button").GetIndex(i).Get("Onclick").String()
				b.Href, _ = js.Get("Button").GetIndex(i).Get("Href").String()
				b.Class, _ = js.Get("Button").GetIndex(i).Get("Class").String()
				tables.Buttons[b.Name] = b
			}
		}

	} else {
		beego.Debug(err)
	}
	err = t.Execute(buff, tables)
	re := buff.String()
	re = strings.Replace(re, "&lt;", "<", -1)
	re = strings.Replace(re, "&gt;", ">", -1)
	re = strings.Replace(re, "&#39;", "'", -1)
	return re
}

func InitHtmlFuc() {
	beego.AddFuncMap("GetListGrid", GetListGrid)
}
